package com.nuanshui.heatedloan.web.credit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.credit.facade.CustomerCreditAuditService;
import com.nuanshui.heatedloan.credit.form.CustomerCreditAuditForm;
import com.nuanshui.heatedloan.credit.model.AuditRequestParam;
import com.nuanshui.heatedloan.credit.model.CreditAuditDetail;
import com.nuanshui.heatedloan.credit.model.CreditAuditInfo;
import com.nuanshui.heatedloan.credit.request.LetterTrialNote;
import com.nuanshui.heatedloan.credit.request.OperationLogContent;
import com.nuanshui.heatedloan.dao.cms.security.FrontUseDaoImpl;
import com.nuanshui.heatedloan.pageable.Pager;
import com.nuanshui.heatedloan.web.ApiController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by wangxw on 2017/7/30.
 */


@RequestMapping("/cms/manage/creditaudit")
@RestController
public class CustomerCreditAuditController extends ApiController {

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private CustomerCreditAuditService customerCreditAuditService;

    @Autowired
    private FrontUseDaoImpl frontUseDaoImpl;

    /***
     *
     * @param type  1 借款列表  2 未分配  3  初审列表  4 终审列表
     * @param pager
     * @param query
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.POST)
    public String list(int type, Pager<CreditAuditInfo> pager, String query){
        AuditRequestParam param=null;
        if(SysStringUtils.isNotEmpty(query)){
            param=JSON.parseObject(query,AuditRequestParam.class);
        }else{
            param=new  AuditRequestParam();
        }
        pager= customerCreditAuditService.getCreditAuditInfo(type,pager,param);
        return success(pager.asHashMap());
    }

    /***
     * 信审订单详情
     * @param orderId
     * @return
     */
    @RequestMapping(value = "detail",method = RequestMethod.POST)
    public String detail(String orderId){
        CreditAuditDetail creditAuditDetail = customerCreditAuditService.getCreditAuditInfoVO(orderId);
        return success(creditAuditDetail);
    }

    /***
     *
     * @param orderId
     * @param orderId  1 提交  2  暂存  3
     * @return
     */
    @RequestMapping(value = "getOperationLog",method = RequestMethod.POST)
    public String getOperationLog(String  orderId ){
        List<OperationLogContent> operationLogContents=customerCreditAuditService.getOperationLog(orderId);
        return success(operationLogContents);
    }

    /***
     *
     * @param data  1  提交 2  暂存  3 内部回退 坑爹需求
     */
    @RequestMapping(value = "create",method = RequestMethod.POST)
    public String create(String data, int type){
        String userId=getCurrentUser().getUser().getMixId();
        LetterTrialNote letterTrialNote= JSON.parseObject(data,LetterTrialNote.class);
        customerCreditAuditService.create(letterTrialNote,type,userId);
        return success();
    }


    @RequestMapping(value = "getUesrByRoleName",method = RequestMethod.POST)
    public String getUesrByRoleName(String name){
        List<Map<String,Object>>  userVO=frontUseDaoImpl.findByRoleName(name);
        return success(userVO);
    }

    /***
     *
     * @param data
     * @param type  1 分配  2 初审退回  3 结案
     * @return
     */
    @RequestMapping(value = "distributionOrder",method = RequestMethod.POST)
    public String distributionOrder(String data,int type,String operationId ){
       List<CustomerCreditAuditForm> customerCreditAuditForms= JSON.parseArray(data,CustomerCreditAuditForm.class);
       customerCreditAuditService.distributionOrder(customerCreditAuditForms,type,operationId);
        return success();
    }

    /***
     * 终审回退
     * @return
     * type 4 代表终审回退
     */
    @RequestMapping(value = "finalBack",method = RequestMethod.POST)
    public String finalBack(String data,int type,String operationId ){
        List<CustomerCreditAuditForm> customerCreditAuditForms= JSON.parseArray(data,CustomerCreditAuditForm.class);
        customerCreditAuditService.finalBack(customerCreditAuditForms,type,operationId);
       return success();
    }




}
